#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  insmod_test
# Version	:  1.0
# Date		:  2023/12/06
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinos.com.cn 
# History	:     
#              Version 1.0, 2023/12/06
# Function	:  内核模块加载
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="内核模块加载"

# 目标文件
MakeFile="Makefile"
InsmodFile="insmod"
BigDma="bigdma"
YzDma="yzdma"
LsDma="lsdma"
ReQuest="request"
HrTimer="hrtimer"
TaskLet="tasklet"
TaskHi="taskhi"
Irq="irq"
InterRupt="interrupt"
Rcu="rcu"
RwClock="rwclock"
MeMory="memory"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        cp -r ${KERNEL_SRC_DIR_KERNEL}/${InsmodFile} ${TmpTestDir_LTFLIB}
        CommRetParse_FailDiy_LTFLIB ${ERROR} "复制目录失败${KERNEL_SRC_DIR_KERNEL}/${InsmodFile}"
	        
        chmod -R 777 ${TmpTestDir_LTFLIB}/${InsmodFile}
        CommRetParse_LTFLIB "成功赋权目录${TmpTestDir_LTFLIB}/${InsmodFile}"
     
        cd ${TmpTestDir_LTFLIB}/${InsmodFile}
        make clean
        CommRetParse_LTFLIB "成功清理环境"
        make
        CommRetParse_LTFLIB "成功编译${MakeFile}"
        return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        Debug_LLE "rm -rf ${TmpTestDir_LTFLIB}/${InsmodFile}"
	rm -rf ${TmpTestDir_LTFLIB}/${InsmodFile}
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1
        testcase_2
        testcase_3
        testcase_4
        testcase_5
        testcase_6
        testcase_7
        return $TPASS
}


## TODO ： 内核模块加载
# 支持 DMA 内存的申请和释放，包括流式 DMA、-致性 DMA 以及大内存 DMA
testcase_1(){   
        insmod ${BigDma}.ko
        CommRetParse_LTFLIB "成功加载模块${BigDma}.ko" 

        lsmod |grep ${BigDma}
        CommRetParse_LTFLIB "查看模块${BigDma}" 

        rmmod ${BigDma}
        CommRetParse_LTFLIB "删除模块${BigDma}"

        insmod ${YzDma}.ko
        CommRetParse_LTFLIB "成功加载模块${YzDma}.ko"

        lsmod |grep ${YzDma}
        CommRetParse_LTFLIB "查看模块${YzDma}"

        rmmod ${YzDma}
        CommRetParse_LTFLIB "删除模块${YzDma}"

        insmod ${LsDma}.ko
        CommRetParse_LTFLIB "成功加载模块${LsDma}.ko"

        lsmod |grep ${LsDma}
        CommRetParse_LTFLIB "查看模块${LsDma}"

        rmmod ${LsDma}
        CommRetParse_LTFLIB "删除模块${LsDma}"
}

# 支持硬件中断号和软件中断号的映射、注册和处理
testcase_2(){
        insmod ${ReQuest}.ko
        CommRetParse_LTFLIB "成功加载模块${ReQuest}.ko"

        lsmod |grep ${ReQuest}
        CommRetParse_LTFLIB "查看模块${ReQuest}"

        local insrequest="my_platform driver loaded"
        dmesg|grep ${insrequest}|head -n 20
        CommRetParse_LTFLIB "加载${ReQuest}模块后查看dmesg信息"

        local rmrequest="my_platform driver unloaded"
        rmmod ${ReQuest}
        dmesg|grep "${rmrequest}"|head -n 20
        CommRetParse_LTFLIB "删除模块${ReQuest}"
}

# 支持高精度时钟中断、软中断和 tasklet 下半部中断处理
testcase_3(){
        insmod ${HrTimer}.ko
        CommRetParse_LTFLIB "成功加载模块${HrTimer}.ko"

        lsmod |grep ${HrTimer}
        CommRetParse_LTFLIB "查看模块${HrTimer}"

        dmesg|grep ${HrTimer}|head -n 20
        CommRetParse_LTFLIB "加载${HrTimer}模块后查看dmesg信息"

        rmmod ${HrTimer}
        CommRetParse_LTFLIB "删除模块${HrTimer}"

        insmod ${TaskLet}.ko
        CommRetParse_LTFLIB "成功加载模块${TaskLet}.ko"

        lsmod |grep ${TaskLet}
        CommRetParse_LTFLIB "查看模块${TaskLet}"

        local instasklet="Tasklet example module  loaded"
        dmesg|grep "${instasklet}"|head -n 20
        CommRetParse_LTFLIB "加载${TaskLet}模块后查看dmesg信息"

        local rmtasklet="Tasklet example module unloaded"
        rmmod ${TaskLet}
        dmesg|grep "${rmtasklet}"|head -n 20
        CommRetParse_LTFLIB "删除模块${TaskLet}"

        insmod ${TaskHi}.ko
        CommRetParse_LTFLIB "成功加载模块${TaskHi}.ko"

        lsmod |grep ${TaskHi}
        CommRetParse_LTFLIB "查看模块${TaskHi}"

        local instaskhi="Tasklet_Hi example module loaded"
        dmesg|grep "${instaskhi}"|head -n 20
        CommRetParse_LTFLIB "加载${TaskHi}模块后查看dmesg信息"

        local rmtaskhi="Tasklet_Hi example module unloaded"
        rmmod ${TaskHi}
        dmesg|grep "${rmtaskhi}"|head -n 20
        CommRetParse_LTFLIB "删除模块${TaskHi}"
}

# 支持中断使能、屏蔽、亲和力处理以及中断抢占
testcase_4(){
        Info_LLE "要求此模块不能加载"
        insmod ${Irq}.ko
        CommRetParse_LTFLIB "加载模块失败为成功${Irq}.ko" "False" "yes"

}

# 支持中断工作队列处理，包括工作队列创建、初始化、调度和回收等
testcase_5(){
        Info_LLE "要求此模块不能加载"
        insmod ${InterRupt}.ko
        CommRetParse_LTFLIB "加载模块失败为成功${InterRupt}.ko" "False" "yes"
}

# 支持读写锁、RCU 原子操作
testcase_6(){
        insmod ${Rcu}.ko
        CommRetParse_LTFLIB "成功加载模块${Rcu}.ko"

		sleep 3

        lsmod |grep ${Rcu}
        CommRetParse_LTFLIB "查看模块${Rcu}"

        local insrcu="Reader thread: val="
        dmesg|grep "${insrcu}"|head -n 20
        CommRetParse_LTFLIB "加载${Rcu}模块后查看dmesg信息"

        local rmrcu="RCU Atomic Example Destroyed"
        rmmod ${Rcu}
        dmesg|grep "${rmrcu}"|head -n 20
        CommRetParse_LTFLIB "删除模块${Rcu}"

        insmod ${RwClock}.ko
        CommRetParse_LTFLIB "成功加载模块${RwClock}.ko"

        lsmod |grep ${RwClock}
        CommRetParse_LTFLIB "查看模块${RwClock}"

        local insrwclock="reader: reading"
        dmesg|grep "${insrwclock}"|head -n 20
        CommRetParse_LTFLIB "加载${RwClock}模块后查看dmesg信息"

        local rmrwclock="RW semaphore module exit"
        rmmod ${RwClock}
        dmesg|grep "${rmrwclock}"|head -n 20
        CommRetParse_LTFLIB "删除模块${RwClock}"
}

# 支持内存屏障操作
testcase_7(){
        insmod ${MeMory}.ko
        CommRetParse_LTFLIB "成功加载模块${MeMory}.ko"

        lsmod |grep ${MeMory}
        CommRetParse_LTFLIB "查看模块${MeMory}"

        local insmemory="Data read from shared memory"
        dmesg|grep "${insmemory}"|head -n 20
        CommRetParse_LTFLIB "加载${MeMory}模块后查看dmesg信息"

        local rmmemory="Module unloaded"
        rmmod ${MeMory}
        dmesg|grep "${rmmemory}"|head -n 20
        CommRetParse_LTFLIB "删除模块${MeMory}"
}

#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
